﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using AForge.Imaging.Filters;
using BrainTechLLC;
using ImageAppViewModels;
using BrainTechLLC.WPF;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for ShowAvailableFilters.xaml
    /// </summary>
    public partial class ShowAvailableFilters : UserControl
    {
        public event EventHandler<ObjectEventArgs<string>> WantAddFilter;
        TypeViewModelCollection _viewModel;

        public ShowAvailableFilters()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ShowAvailableFilters_Loaded);
        }

        void ShowAvailableFilters_Loaded(object sender, RoutedEventArgs e)
        {
            var filterTypes = AllFilters.ReadAllFilters();
            filterTypes.RemoveAll((t) => t.GetConstructor(System.Type.EmptyTypes) == null);
            _viewModel = new TypeViewModelCollection();

            foreach (var type in filterTypes)
            {
                _viewModel.Add(new TypeViewModel(type));
            }

            this.DataContext = _viewModel;
            this.lstFilterItems.ItemsSource = _viewModel;
            InvalidateVisual();
        }

        public void ClearSelected()
        {
            lstFilterItems.SelectedItems.Clear();
        }

        public List<TypeViewModel> GetSelectedTypes()
        {
            List<TypeViewModel> results = new List<TypeViewModel>();

            foreach (var item in lstFilterItems.SelectedItems)
            {
                results.Add(item as TypeViewModel);
            }

            return results;
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount > 1)
            {
                TextBlock tb = sender as TextBlock;

                if (WantAddFilter != null)
                {
                    WantAddFilter(this, new ObjectEventArgs<string>(tb.Text));
                }
            }
        }
    }
}
